package day018.homework.p3;

public class TicketTest implements Runnable {
    private static int ticket = 100;

    @Override
    public void run() {
        while (true) {
            if (buyTicket1()) {
                break;
            }
        }
    }

    public synchronized boolean buyTicket1() {
        if (ticket <= 0) {
            return true;
        } else {
            ticket--;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 售出一张票，剩余票数：" + ticket);
            return false;
        }
    }

    public synchronized static boolean buyTicket2() {
        if (ticket <= 0) {
            return true;
        } else {
            ticket--;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "售出一张票，剩余票数：" + ticket);
            return false;
        }
    }
}
